//
//  CYCardListCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/17.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYCardListCell.h"

#import <UIImageView+WebCache.h>



@implementation CYCardListCellObject

@end



@interface CYCardListCell ()

@property(nonatomic, strong)UIImageView *backgroundImageView;
@property(nonatomic, strong)UIView *bankIconBackgroundImageView;
@property(nonatomic, strong)UIImageView *bankIconImageView;
@property(nonatomic, strong)UILabel *bankNameLabel;
@property(nonatomic, strong)UILabel *cardNumberLabel;
@property(nonatomic, strong)UILabel *onceLimitLabel;
@property(nonatomic, strong)UILabel *totalLimitLabel;

@end


@implementation CYCardListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        
        self.backgroundImageView = [[UIImageView alloc] init];
        self.backgroundImageView.layer.masksToBounds = YES;
        self.backgroundImageView.layer.cornerRadius = 4;
        [self.contentView addSubview:self.backgroundImageView];
        
        self.bankIconBackgroundImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 39, 39)];;
        self.bankIconBackgroundImageView.backgroundColor = [UIColor whiteColor];
        self.bankIconBackgroundImageView.layer.masksToBounds = YES;
        self.bankIconBackgroundImageView.layer.cornerRadius = self.bankIconBackgroundImageView.width / 2;
        [self.contentView addSubview:self.bankIconBackgroundImageView];
        
        self.bankIconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 33, 33)];
        self.bankIconImageView.layer.masksToBounds = YES;
        self.bankIconImageView.layer.cornerRadius = self.bankIconImageView.width / 2;
        [self.contentView addSubview:self.bankIconImageView];
        
        self.bankNameLabel = [[UILabel alloc] init];
        self.bankNameLabel.font = [UIFont systemFontOfSize:16];
        self.bankNameLabel.textColor = HEXCOLOR(@"FFFFFF");
        [self.contentView addSubview:self.bankNameLabel];
        
        self.cardNumberLabel = [[UILabel alloc] init];
        self.cardNumberLabel.font = [UIFont systemFontOfSize:24];
        self.cardNumberLabel.textColor = HEXCOLOR(@"FFFFFF");
        [self.contentView addSubview:self.cardNumberLabel];
        
        self.onceLimitLabel = [[UILabel alloc] init];
        self.onceLimitLabel.font = [UIFont systemFontOfSize:12];
        self.onceLimitLabel.alpha = 0.8;
        self.onceLimitLabel.textColor = HEXCOLOR(@"FFFFFF");
        [self.contentView addSubview:self.onceLimitLabel];
        
        self.totalLimitLabel = [[UILabel alloc] init];
        self.totalLimitLabel.font = [UIFont systemFontOfSize:12];
        self.totalLimitLabel.alpha = 0.8;
        self.totalLimitLabel.textColor = HEXCOLOR(@"FFFFFF");
        [self.contentView addSubview:self.totalLimitLabel];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 123;
}

- (BOOL)shouldUpdateCellWithObject:(CYCardListCellObject *)object
{
    [self.bankIconImageView sd_setImageWithURL:object.bankIcon.tc_url placeholderImage:APP_SQUARE_PLACEHOLDER];
    
    self.bankNameLabel.text = object.bankName;
    
    self.cardNumberLabel.text = object.cardNumber;
    
    self.onceLimitLabel.text = object.onceLimit;
    
    self.totalLimitLabel.text = object.totalLimit;
    
    self.backgroundImageView.image = object.backgroundImage;
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.contentView.width = self.width - 40;
    self.contentView.height = self.height - 20;
    self.contentView.centerY = self.height / 2;
    self.contentView.centerX = self.width / 2;
    
    self.backgroundImageView.frame = self.contentView.bounds;
    
    self.bankIconBackgroundImageView.left = 15;
    self.bankIconBackgroundImageView.top = 15;
    
    self.bankIconImageView.center = self.bankIconBackgroundImageView.center;
    
    [self.bankNameLabel sizeToFit];
    self.bankNameLabel.left = 62;
    self.bankNameLabel.centerY = 24;
    
    [self.cardNumberLabel sizeToFit];
    self.cardNumberLabel.left = 62;
    self.cardNumberLabel.centerY = 49;
    
    [self.onceLimitLabel sizeToFit];
    self.onceLimitLabel.left = 62;
    self.onceLimitLabel.centerY = 84;
    
    [self.totalLimitLabel sizeToFit];
    self.totalLimitLabel.left = self.onceLimitLabel.right + 7;
    self.totalLimitLabel.centerY = 84;
}

@end
